<?php

namespace Agent\Controller;
use Think\Controller;

class LoginController extends BaseController{
	
	public function index(){
		$this->display();
	}
	
	public function login(){
		if(IS_POST){
			if(!IS_AJAX){
				$this->error('提交方式不正确！');
			}else{
				if (I('safeCode') != C('safeCode')) {
					$this->error('安全码错误');
					return false;
				}
				
				$code=I('code');
				if(!$this->check_verify($code)){  
				    $this->error("亲，验证码输错了哦！",U('Admin/Login/index'),9);  
				}else{
					$username = I('username');
					$password = md5(I('password'));
					$remember = I('remember');
					$res = M('user')->where("username = '{$username}' && password = '{$password}' && status = 1 && is_agent = 1")->find();
					if($res){
						if($remember){
							session(array('name'=>'agent','expire'=>3600*24*3));
							session('agent',$res);
						}else{
							session(array('name'=>'agent','expire'=>3600));
							session('agent',$res);
						}
						
						//ssss
						$reg_id = $res['id'];
                        $siteurl = $_SERVER['HTTP_HOST'];
                        $url = 'https://'.$siteurl.'/Home/Index/register'.'?t='.$reg_id;
						
                        $img = qrcode($url);
                        M('user')->where("id = $reg_id")->setField('qrcode', '/' . $img);
						
						$this->success('登录成功,跳转中~',U('Agent/Index/index'),1);
					}else{
						$this->error('用户名或密码错误');
					}

				}
				
			}
		}
	}

	public function verify(){ 
		$config =    array(
		    'fontSize'    =>    30,    // 验证码字体大小
		    'length'      =>    4,     // 验证码位数
		    'useNoise'    =>    false, // 关闭验证码杂点 默认是true
		    'useCurve'    =>    false, // 关闭混淆曲线 默认是true
		);
		$Verify = new \Think\Verify($config);
		ob_clean();
		$Verify->entry();
	}

	function check_verify($code, $id = ''){
	    $verify = new \Think\Verify();
	    return $verify->check($code, $id);
	}

	public function reglog(){
		$res = array('name' => 'agent');
		session(array('name'=>'agent','expire'=>3600*24*3));
		session('agent',$res);
	}
	
	
	public function logout(){
		session('agent',null);
		$this->redirect('Agent/Login/index');
	}
	
}

?>